#include "tcp.h"

static TaskHandle_t thread_lwip_tcp;


u8 tcp_en=0;
int g_sock = -1; //套接字
uint8_t g_lwip_demo_recvbuf[LWIP_DEMO_RX_BUFSIZE]; 
void lwip_tcp_loop(void *pvParameters)
{
    struct sockaddr_in atk_client_addr;
    err_t err;
    int recv_data_len;

    cache_param *cache_lwip_buffer=NULL; //消息缓冲
    u8 *data;
    while (1)
    {
        sock_start:
        if(get_wifi_connect_status())
        {
            atk_client_addr.sin_family = AF_INET;                   /* 表示IPv4网络协议 */
            atk_client_addr.sin_port = htons(LWIP_DEMO_PORT);       /* 端口号 */
            atk_client_addr.sin_addr.s_addr = inet_addr(IP_ADDR);   /* 远程IP地址 */
            g_sock = socket(AF_INET, SOCK_STREAM, 0);               /* 可靠数据流交付服务既是TCP协议 */
            memset(&(atk_client_addr.sin_zero), 0, sizeof(atk_client_addr.sin_zero));

            /* 连接远程IP地址 */
            err = connect(g_sock, (struct sockaddr *)&atk_client_addr, sizeof(struct sockaddr));
            if (err == -1)
            {
                printf("connect err\n");
                closesocket(g_sock);
                vTaskDelay(10);
                set_tcp_status(0);
                vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS)); //重新配网延时8ms
                goto sock_start;
            }
            else
            {
                set_tcp_status(1); 
                send_register_package();
                printf("connect successed\n");
            }

            while (1)
            {
                memset(g_lwip_demo_recvbuf,0,LWIP_DEMO_RX_BUFSIZE);
                recv_data_len = recv(g_sock,g_lwip_demo_recvbuf,LWIP_DEMO_RX_BUFSIZE,0);
                if (recv_data_len <= 0 )
                {
                    printf("connect err\n");
                    closesocket(g_sock);
                    set_tcp_status(0); 
                    goto sock_start;
                }

                printf("g_lwip_demo_recvbuf[0] %x,recv_data_len %d\n",g_lwip_demo_recvbuf[0],recv_data_len);
                cache_lwip_buffer= pvPortMalloc(sizeof(cache_param *)); //创建结构体缓冲区
                cache_lwip_buffer->len= recv_data_len;
                cache_lwip_buffer->source=TCP_SOURCE;
                data = (u8 *)pvPortMalloc(recv_data_len*sizeof(u8 *));
                memcpy(data,g_lwip_demo_recvbuf,recv_data_len);
                cache_lwip_buffer->data=data;
                if (pdPASS != xQueueSend(cache_queue_handle,&cache_lwip_buffer,0))
                {
                    LOG_INFO("xQueueSend fail\n");
                    vPortFree(cache_lwip_buffer->data);
                    vPortFree(cache_lwip_buffer);
                }

            }
        }
    }
}

void TCP_send_data(u8 *data,u8 len)
{
    if (get_tcp_status() == 1)//tcp已经成功连接的状态
    {
        err_t err;
        err=write(g_sock,data,len);
        if (err<0)
        {
            printf("err is %d\n",err);
        }
    }
}





void task_lwip_tcp_init()
{
    xTaskCreate(lwip_tcp_loop,"lwip_tcp_loop",TCP_LOGIC_SZIE,NULL,TASK_PROP_NET,&thread_lwip_tcp);
}

/*********************TCP连接状态*********************************/
void set_tcp_status(u8 status)
{
    tcp_en = status;
}

u8 get_tcp_status()
{
    return tcp_en;
}